//
//  MarketGoodsCell.m
//  yigou
//
//  Created by wangdianlei on 2017/1/5.
//  Copyright © 2017年 hanyiwang. All rights reserved.
//
#import "MarketGoodsCell.h"

@implementation MarketGoodsCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
//    [self.left.layer setBorderWidth:1];
//    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
//    CGColorRef colorref = CGColorCreate(colorSpace, (CGFloat[]){0.9, 0.9, 0.9, 1});
//    [self.layer setBorderColor:colorref];
//    CGColorRelease(colorref);
//    CGColorSpaceRelease(colorSpace);
    
    self.lprice = [UILabel new];
    [self.left addSubview:_lprice];
    _lprice.font = [FontTool fontWithSize:13];
    _lprice.textColor = [UIColor redColor];
    _lprice.textAlignment = NSTextAlignmentLeft;
    [_lprice mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.left.mas_bottom).with.offset(-10);
        make.left.equalTo(self.left.mas_left).with.offset(2);
        make.right.equalTo(self.left.mas_right).with.offset(0);
        make.height.mas_equalTo(20);
    }];
    
    NSString *changeStr = @"一口价: ¥244.00";
//    NSString *subStr = @"一口价: ";
//    NSRange range = [changeStr rangeOfString:subStr];
//    NSMutableAttributedString *strAttribute = [[NSMutableAttributedString alloc] initWithString:changeStr];
//    [strAttribute addAttribute:NSForegroundColorAttributeName value:[UIColor lightGrayColor] range:range];
//    [strAttribute addAttribute:NSFontAttributeName value:[FontTool fontWithSize:11] range:range];
//    _lprice.attributedText = strAttribute;
    _lprice.text = changeStr;
    
    self.ltag1 = [UILabel new];
    _ltag1.text = @"包邮";
    [self.left addSubview:_ltag1];
    _ltag1.backgroundColor = [UIColor lightGrayColor];
    _ltag1.layer.masksToBounds = YES;
    _ltag1.layer.cornerRadius = 2.f;
    _ltag1.font = [FontTool fontWithSize:10];
    _ltag1.textColor = [UIColor whiteColor];
    _ltag1.textAlignment = NSTextAlignmentCenter;
    [_ltag1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.lprice.mas_top).with.offset(-3);
        make.left.equalTo(self.left.mas_left).with.offset(2);
        make.width.mas_equalTo(25);
        make.height.mas_equalTo(15);
    }];
    
    self.laddr = [UILabel new];
    self.laddr.text = @"";
    [self.left addSubview:self.laddr];
    self.laddr.font = [FontTool fontWithSize:11];
    self.laddr.textAlignment = NSTextAlignmentCenter;
    [self.laddr mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.lprice.mas_top).with.offset(-3);
        make.right.equalTo(self.left.mas_right).with.offset(-3);
        make.width.mas_equalTo(30);
        make.height.mas_equalTo(15);
    }];
//    self.ltag2 = [UILabel new];
//    _ltag2.text = @"7天退换";
//    [self.left addSubview:_ltag2];
//    _ltag2.backgroundColor = [UIColor lightGrayColor];
//    _ltag2.layer.masksToBounds = YES;
//    _ltag2.layer.cornerRadius = 2.f;
//    _ltag2.font = [FontTool fontWithSize:10];
//    _ltag2.textColor = [UIColor whiteColor];
//    _ltag2.textAlignment = NSTextAlignmentCenter;
//    [_ltag2 mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.bottom.equalTo(self.lprice.mas_top).with.offset(-3);
//        make.left.equalTo(_ltag1.mas_right).with.offset(5);
//        make.width.mas_equalTo(40);
//        make.height.mas_equalTo(15);
//    }];
    
    self.lgoodname = [UILabel new];
    _lgoodname.text = @"思想背后的世界";
    [self.left addSubview:_lgoodname];
    _lgoodname.font = [FontTool fontWithSize:13];
    _lgoodname.textColor = [UIColor blackColor];
    _lgoodname.textAlignment = NSTextAlignmentLeft;
    [_lgoodname mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(_ltag1.mas_top).with.offset(-3);
        make.left.equalTo(_left.mas_left).with.offset(2);
        make.right.equalTo(_left.mas_right).with.offset(0);
        make.height.mas_equalTo(20);
    }];
    
    _limg = [[UIImageView alloc] init];
    _limg.layer.masksToBounds = YES;
    _limg.contentMode = UIViewContentModeScaleAspectFill;
    _limg.image = [UIImage imageNamed:@"placeholder.png"];
    [self.left addSubview:_limg];
    
    CGFloat blankW = (ScreenWidth - 20 * 2 - 10) / 2;
    CGFloat blankH = 307 - 50 - 15 - 20 - 3 - 15 - 3 - 20 - 10 - 3;
    CGFloat imgwidth = (blankW > blankH) ? blankH : blankW;
    _limg.frame = CGRectMake(0, (blankH - imgwidth) / 2, imgwidth, imgwidth);
    
    self.rprice = [UILabel new];
    [self.right addSubview:_rprice];
    _rprice.font = [FontTool fontWithSize:13];
    _rprice.textColor = [UIColor redColor];
    _rprice.textAlignment = NSTextAlignmentLeft;
    [_rprice mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.right.mas_bottom).with.offset(-10);
        make.left.equalTo(self.right.mas_left).with.offset(2);
        make.right.equalTo(self.right.mas_right).with.offset(0);
        make.height.mas_equalTo(20);
    }];
    
//    NSMutableAttributedString *strAttribute2 = [[NSMutableAttributedString alloc] initWithString:changeStr];
//    [strAttribute2 addAttribute:NSForegroundColorAttributeName value:[UIColor lightGrayColor] range:range];
//    [strAttribute2 addAttribute:NSFontAttributeName value:[FontTool fontWithSize:11] range:range];
//    _rprice.attributedText = strAttribute2;
    _rprice.text = changeStr;
    
    self.rtag1 = [UILabel new];
    _rtag1.text = @"包邮";
    [self.right addSubview:_rtag1];
    _rtag1.backgroundColor = [UIColor lightGrayColor];
    _rtag1.layer.masksToBounds = YES;
    _rtag1.layer.cornerRadius = 2.f;
    _rtag1.font = [FontTool fontWithSize:10];
    _rtag1.textColor = [UIColor whiteColor];
    _rtag1.textAlignment = NSTextAlignmentCenter;
    [_rtag1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.rprice.mas_top).with.offset(-3);
        make.left.equalTo(self.right.mas_left).with.offset(2);
        make.width.mas_equalTo(25);
        make.height.mas_equalTo(15);
    }];
    
    self.raddr = [UILabel new];
    self.raddr.text = @"";
    [self.right addSubview:self.raddr];
    self.raddr.font = [FontTool fontWithSize:11];
    self.raddr.textAlignment = NSTextAlignmentCenter;
    [self.raddr mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.rprice.mas_top).with.offset(-3);
        make.right.equalTo(self.right.mas_right).with.offset(-3);
        make.width.mas_equalTo(30);
        make.height.mas_equalTo(15);
    }];
    
//    self.rtag2 = [UILabel new];
//    _rtag2.text = @"7天退换";
//    [self.right addSubview:_rtag2];
//    _rtag2.backgroundColor = [UIColor lightGrayColor];
//    _rtag2.layer.masksToBounds = YES;
//    _rtag2.layer.cornerRadius = 2.f;
//    _rtag2.font = [FontTool fontWithSize:10];
//    _rtag2.textColor = [UIColor whiteColor];
//    _rtag2.textAlignment = NSTextAlignmentCenter;
//    [_rtag2 mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.bottom.equalTo(self.rprice.mas_top).with.offset(-3);
//        make.left.equalTo(_rtag1.mas_right).with.offset(5);
//        make.width.mas_equalTo(40);
//        make.height.mas_equalTo(15);
//    }];
    
    self.rgoodname = [UILabel new];
    _rgoodname.text = @"思想背后的世界";
    [self.right addSubview:_rgoodname];
    _rgoodname.font = [FontTool fontWithSize:13];
    _rgoodname.textColor = [UIColor blackColor];
    _rgoodname.textAlignment = NSTextAlignmentLeft;
    [_rgoodname mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(_rtag1.mas_top).with.offset(-3);
        make.left.equalTo(_right.mas_left).with.offset(2);
        make.right.equalTo(_right.mas_right).with.offset(0);
        make.height.mas_equalTo(20);
    }];
    
    _rimg = [[UIImageView alloc] init];
    _rimg.layer.masksToBounds = YES;
    _rimg.contentMode = UIViewContentModeScaleAspectFill;
    _rimg.image = [UIImage imageNamed:@"placeholder.png"];
    [self.right addSubview:_rimg];
    _rimg.frame = CGRectMake(0, (blankH - imgwidth) / 2, imgwidth, imgwidth);
    
    self.lrinter.constant = 10 + (blankW - imgwidth);
    self.rtrail.constant = 20 - (blankW - imgwidth);
    
    self.left.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickLeft:)];
    tap.numberOfTapsRequired = 1;
    [self.left addGestureRecognizer:tap];
    self.right.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickRight:)];
    tap2.numberOfTapsRequired = 1;
    [self.right addGestureRecognizer:tap2];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)fillWithData:(MMMarketNew *)ldata right:(MMMarketNew *)rdata {
    if (ldata == nil) {
        self.left.hidden = YES;
    } else {
        self.left.hidden = NO;
        self.lgoodname.text = ldata.goods_name;
        self.laddr.text = ldata.goods_addr;
        self.lprice.text = [NSString stringWithFormat:@"¥%@", ldata.market_price];//ldata.market_price;
        
        NSString *imgurl = @"";
        imgurl = ldata.goods_pic_url;
        NSArray *array = [ldata.goods_pic_url componentsSeparatedByString:@","];
        if (array.count > 0) {
            imgurl = array[0];
        }
        [self.limg sd_setImageWithURL:[NSURL URLWithString:imgurl]];
        NSString *cl = ldata.chenluo_show;
        if (cl.length == 0) {
            cl = @" 7天退换 ";
        }
        _ltag1.text = cl;
        [_ltag1 mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo([UILabel getWidthWithTitle:cl font:[FontTool fontWithSize:10]]);
        }];
    }
    if (rdata == nil) {
        self.right.hidden = YES;
    } else {
        self.right.hidden = NO;
        self.rgoodname.text = rdata.goods_name;
        self.raddr.text = rdata.goods_addr;
        self.rprice.text = [NSString stringWithFormat:@"¥%@", rdata.market_price];;
        NSString *imgurl = @"";
        imgurl = rdata.goods_pic_url;
        NSArray *array = [rdata.goods_pic_url componentsSeparatedByString:@","];
        if (array.count > 0) {
            imgurl = array[0];
        }
        [self.rimg sd_setImageWithURL:[NSURL URLWithString:imgurl]];
        NSString *cl = ldata.chenluo_show;
        if (cl.length == 0) {
            cl = @" 7天退换 ";
        }
        _rtag1.text = cl;
        [_rtag1 mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo([UILabel getWidthWithTitle:cl font:[FontTool fontWithSize:10]]);
        }];
    }    
}

- (void)clickLeft:(UITapGestureRecognizer*)recognizer {
    if (self.delegate && [self.delegate respondsToSelector:@selector(clickMarketGoodCell:isLeft:myType:)]) {
        [self.delegate clickMarketGoodCell:self.index isLeft:YES myType:self.type];
    }
}

- (void)clickRight:(UITapGestureRecognizer*)recognizer {
    if (self.delegate && [self.delegate respondsToSelector:@selector(clickMarketGoodCell:isLeft:myType:)]) {
        [self.delegate clickMarketGoodCell:self.index isLeft:NO myType:self.type];
    }
}

@end
